import request from '@/utils/request'
import { getToken, setToken, removeToken,setCookie,getCookie } from "@/utils/auth";
import { getCategory } from "@/api/system/category"

// 查询科研平台-研究机构列表
export function listUnit(query) {
  return request({
    url: '/research/unit/list',
    method: 'get',
    params: query
  })
}

export function getCheckStatusCount(query) {
  return request({
    url: '/research/unit/getCheckStatusCount',
    method: 'get',
    params: query
  })
}

// 查询科研平台-研究机构详细
export function getUnit(id) {
  return request({
    url: '/research/unit/' + id,
    method: 'get'
  })
}

// 新增科研平台-研究机构
export function addUnit(data) {
  return request({
    url: '/research/unit',
    method: 'post',
    data: data
  })
}

// 修改科研平台-研究机构
export function updateUnit(data) {
  return request({
    url: '/research/unit',
    method: 'put',
    data: data
  })
}

export function editPlatformSummary(data) {
  return request({
    url: '/research/unit/editPlatformSummary',
    method: 'put',
    data: data
  })
}

// 删除科研平台-研究机构
export function delUnit(id) {
  return request({
    url: '/research/unit/' + id,
    method: 'delete'
  })
}

export function addStartWorkflow(data) {
  return request({
    url: '/research/unit/addStartWorkflow',
    method: 'post',
    data: data
  })
}

export function addAndPass(data) {
  return request({
    url: '/research/unit/addAndPass',
    method: 'post',
    data: data
  })
}

export function editStartWorkflow(data) {
  return request({
    url: '/research/unit/editStartWorkflow',
    method: 'put',
    data: data
  })
}

export function editAndPass(data) {
  return request({
    url: '/research/unit/editAndPass',
    method: 'put',
    data: data
  })
}

export function getCurRole() {
  return getCookie('role-key');
}

export function back(proxy) {
  const obj = { path: "/research" };
  proxy.$tab.closeOpenPage(obj);
};

export function getKyryByName(name) {
  return request({
      url: "/kyry/personDetails",
      method: "post",
      data: {name: name},
  });
}

function getCategoryDataKey(data) {
  const keys = Object.keys(data);
  if (keys.includes("codeColumn") && keys.includes("valueColumn")) {
    return ["codeColumn", "valueColumn"];
  } else if (keys.includes("code") && keys.includes("name")) {
    return ["code", "name"];
  } else if (keys.includes("id") && keys.includes("label")) {
    return ["id", "label"];
  } else {
    return [null, null];
  }
}

function handleCategoryData(data, valueKey, labelKey) {
  if (typeof data === "object" && data.length > 0) {
    if (!valueKey || !labelKey) {
      const [vk, lk] = getCategoryDataKey(data[0]);  
      valueKey = vk;
      labelKey = lk;
    }
    return data.map(item => ({value:item[valueKey],label:item[labelKey],children:handleCategoryData(item.children,valueKey,labelKey)}));
  }
}

export function getCategoryData(dataRef, query, addEmptyAtFirst, cb) {
  getCategory(query).then(response => {
      dataRef.value = handleCategoryData(response.data)
      if (addEmptyAtFirst === true){
        dataRef.value.unshift({value:'', label:''});
      } else if (typeof addEmptyAtFirst === 'object') {
        dataRef.value.unshift(addEmptyAtFirst);
      }
      cb?cb():'';
  });
}

export function getOverPastYearOptions(num, addEmptyAtFirst) {
  let arr = addEmptyAtFirst ? [{label: '', value: ''}] : [];
  if (typeof num !== 'number') {
    return arr;
  }
  let currentYear = new Date().getFullYear();
  for (let index = 0; index < num; index++) {
    arr.push({label: currentYear - index, value: currentYear - index});
  }
  return arr;
}

export function getFlatTree(data, res) {
  if (!data || !Array.isArray(data) || !res || !Array.isArray(res)) {
    return
  }

  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    res.push(element);
    if (Array.isArray(element.children)) {
      getFlatTree(element.children, res)
    }
  }
}
